<?php
namespace app\api\controller\inventory;

use app\api\controller\Base;
use app\api\controller\Pub;
use app\api\controller\mytool\Sendmessage;
use app\common\model\orderdata\Storageareachange;
use app\common\model\orderdata\StorageAreaGoods;
use app\common\model\orderdata\Goods;

class Reviewadjust extends Base
{
    function list() {
        $data = input('get.');
        $data = init_where($data);
//        $data['order'] = 'GoodId desc';
        //        $area_good = StorageAreaGoods::where('StorageAreaId', $data['area_id'])->select()->toArray();
        //        $good_id = array_column($area_good, 'GoodId');
        //        $good = Goods::where('GoodId', 'in', $good_id)->select()->toArray();
        $list                = Storageareachange::lists($data);
        $list['tr']['title'] = ['提交时间', '提交人', '货物名称', '微调id', '重量'];
        $list['tr']['key']   = ['UpTime', 'Username', 'GoodName', 'StorageAreaChangeId', 'Weight'];
//        $list['tr']['show']=[true,true,true,true,true,false];
        //        $list['tr']['type'] = CostType::getlist('costview');
        //        $list['tr']['typekey'] = ['id'=>'TypeId','text'=>'TypeName'];

        return myok($list);
    }

    public function detailsList()
    {
        $param  = input('get.');
        $model  = new Storageareachange();
        $option = come_model_option($param, $model);
        $rdata  = $option['rdata'];
        $data   = $option['data'];
        $rdata  = $rdata->select();
//        $rdata->visible($visible);
        $ndata['list']     = $rdata;
        $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        $list              = $ndata;
        foreach ($list['list'] as &$v) {
            $user_name     = $v->user->Name ?? '';
            $good_name     = $v->good->Name ?? '';
            $v->UpUserName = $user_name;
            $v->GoodName   = $good_name;
        }
        unset($v);
        return myok($list);
    }

    /**
     * 通过/拒绝
     * @return \think\response\Json
     */
    public function review()
    {
        $param = $this->request->post();
        if (empty($param['StorageAreaChangeId']) || empty($param['Status'])) {
            return myerr('非法请求');
        }
        try {
            $info = Storageareachange::where('StorageAreaChangeId', $param['StorageAreaChangeId'])->findOrEmpty();
            if ($info->isEmpty()) {
                return myerr('非法请求');
            }
            if($info->Status!==1){
            	return myerr('申请已处理');
            }
            if ($param['Status'] == Storageareachange::STATEOK) {
            	//库存货物区域表
                $sb         = StorageAreaGoods::where('StorageAreaGoodsId', $info['StorageAreaGoodsId'])->find();
                $sb->Weight = $sb->Weight + $info['Weight'];
                $sb->save();
                //库存表
                $sb = Goods::get($sb['GoodId']);
                $sb->TotalWeight = $sb->TotalWeight + $info['Weight'];
                $sb->save();
                //发通知给库管和股东
                Sendmessage::reviewadjust(2,$param['StorageAreaChangeId'],$info);
                Sendmessage::reviewadjust(21,$param['StorageAreaChangeId'],$info);
            }else if($param['Status']==Storageareachange::STATENO){
                //拒绝后发通知给库管
                Sendmessage::reviewadjust(3,$param['StorageAreaChangeId'],$info);
            }else{
                return myerr('状态错误');
            }
            Storageareachange::where('StorageAreaChangeId', $param['StorageAreaChangeId'])
                ->update([
                	'Status'       => $param['Status'],
                   	'ConfirmUserId' => Pub::userid(),
                    'ConfirmTime'   => date('Y-m-d H:i:s', time()),
                ]);

            return myok();
        } catch (Exception $e) {
            return myerr('操作失败');
        }

    }
}
